package com.outsourcing.bottle.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.MaybeFriendEntry;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.ui.UserSpaceActivity;
import com.outsourcing.bottle.ui.fragment.FriendFragment;
import com.outsourcing.bottle.util.TextUtil;

public class MaybeKnowFriendListAdapter extends BaseAdapter {

	private Context context = null;
	private List<MaybeFriendEntry> mbtfriendList = null;
	private FriendFragment fragment;

	public MaybeKnowFriendListAdapter(Context context, FriendFragment fragment,
			List<MaybeFriendEntry> mbtfriendList) {
		this.context = context;
		this.fragment = fragment;
		this.mbtfriendList = mbtfriendList;
	}

	@Override
	public int getCount() {
		return mbtfriendList.size();
	}

	@Override
	public Object getItem(int position) {
		return mbtfriendList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BTFriendViewHolder holder = null;
		if (convertView == null
				|| ((BTFriendViewHolder) convertView.getTag()).flag != position) {
			holder = new BTFriendViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.bottle_list_avatar_item, null);
			holder.flag = position;
			holder.mAuthorView = (RemoteImageView) convertView.findViewById(R.id.iv_author_photo);
			// content layout
			LinearLayout contentLayout = (LinearLayout) convertView
					.findViewById(R.id.feed_post_body);
			View view = LayoutInflater.from(context).inflate(
					R.layout.myfriend_list_item, null);
			holder.mNickName = (TextView) view.findViewById(R.id.tv_onickname);
			holder.mSex = (ImageView)view.findViewById(R.id.bt_sex);
			holder.mDoing = (TextView) view.findViewById(R.id.tv_odoing);
			holder.mLocation = (TextView) view.findViewById(R.id.tv_olocation);
			holder.mAddFriend = (Button) view.findViewById(R.id.bt_add_friend);

			holder.twitter_layout = (LinearLayout) view
					.findViewById(R.id.user_space_relation_twitter_layout);
			holder.facebook_layout = (LinearLayout) view
					.findViewById(R.id.user_space_relation_facebook_layout);
			holder.weibo_layout = (LinearLayout) view
					.findViewById(R.id.user_space_relation_weibo_layout);
			holder.tencent_layout = (LinearLayout) view
					.findViewById(R.id.user_space_relation_tencent_layout);
			holder.renren_layout = (LinearLayout) view
					.findViewById(R.id.user_space_relation_renren_layout);
			holder.douban_layout = (LinearLayout) view
					.findViewById(R.id.user_space_relation_douban_layout);
			holder.qq_layout = (LinearLayout) view
					.findViewById(R.id.user_space_relation_qq_layout);

			holder.twitter_textview = (TextView) view
					.findViewById(R.id.user_space_relation_twitter_textview);
			holder.facebook_textview = (TextView) view
					.findViewById(R.id.user_space_relation_facebook_textview);
			holder.weibo_textview = (TextView) view
					.findViewById(R.id.user_space_relation_weibo_textview);
			holder.tencent_textview = (TextView) view
					.findViewById(R.id.user_space_relation_tencent_textview);
			holder.renren_textview = (TextView) view
					.findViewById(R.id.user_space_relation_renren_textview);
			holder.douban_textview = (TextView) view
					.findViewById(R.id.user_space_relation_douban_textview);
			holder.qq_textview = (TextView) view
					.findViewById(R.id.user_space_relation_qq_textview);

			contentLayout.addView(view);

			final MaybeFriendEntry maybeFriendEntry = (MaybeFriendEntry) getItem(position);

			try {
				holder.mAuthorView.setDefaultImage(R.drawable.avatar_default_big);
				holder.mAuthorView.setImageUrl(maybeFriendEntry.getMkavatar());
			} catch (Exception e) {
				e.printStackTrace();
				holder.mAuthorView
						.setImageResource(R.drawable.avatar_default_big);
			}
			holder.mAuthorView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
					spaceIntent.putExtra("ouid", maybeFriendEntry.getMkuid());
					context.startActivity(spaceIntent);
				}
			});
			holder.mAddFriend.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					fragment.doCheckApplyFriend(maybeFriendEntry.getMkuid());
				}
			});
			String nickName = null;
			if (maybeFriendEntry.getIsnew() == 1) {
				nickName = "<font color='#DA4A37'>"
						+ maybeFriendEntry.getMknickname() + "</font>";
			}else {
				nickName = "<font color='#3F99D8'>"
						+ maybeFriendEntry.getMknickname() + "</font>";
			}
//			String sex = (((maybeFriendEntry.getMksex()) == 1) ? ("("
//					+ TextUtil.R("login_txt_register_male") + ")") : ("("
//					+ TextUtil.R("login_txt_register_female") + ")"));
			holder.mNickName.setText(Html.fromHtml(nickName));
			int sex_res = maybeFriendEntry.getMksex() == 1 ? R.drawable.male:R.drawable.female;
			holder.mSex.setBackgroundResource(sex_res);
			if (null != maybeFriendEntry.getMkdoing()
					&& maybeFriendEntry.getMkdoing().length() > 0) {
				holder.mDoing.setVisibility(View.VISIBLE);
				String doingTips = "<font color='#000000'>"
						+ TextUtil.R("friend_doing_tips") + "</font>";
				holder.mDoing.setText(Html.fromHtml(doingTips
						+ maybeFriendEntry.getMkdoing()));
			} else {
				holder.mDoing.setVisibility(View.GONE);
			}
			if ((null != maybeFriendEntry.getMkcountry() && maybeFriendEntry
					.getMkcountry().length() > 0)
					|| (null != maybeFriendEntry.getMkcity() && maybeFriendEntry
							.getMkcity().length() > 0)) {
				holder.mLocation.setVisibility(View.VISIBLE);
				String fromTips = TextUtil.R("friend_comfrom_tips");
				holder.mLocation.setText(fromTips
						+ maybeFriendEntry.getMkcity() + " "
						+ maybeFriendEntry.getMkcountry());

			} else {
				holder.mLocation.setVisibility(View.GONE);
			}

			if (null != maybeFriendEntry.getRelation_twitter()
					&& maybeFriendEntry.getRelation_twitter().length() > 0) {
				holder.twitter_layout.setVisibility(View.VISIBLE);
				holder.twitter_textview.setText(maybeFriendEntry
						.getRelation_twitter());
			} else {
				holder.twitter_layout.setVisibility(View.GONE);
			}

			if (null != maybeFriendEntry.getRelation_facebook()
					&& maybeFriendEntry.getRelation_facebook().length() > 0) {
				holder.facebook_layout.setVisibility(View.VISIBLE);
				holder.facebook_textview.setText(maybeFriendEntry
						.getRelation_facebook());
			} else {
				holder.facebook_layout.setVisibility(View.GONE);
			}

			if (null != maybeFriendEntry.getRelation_sina()
					&& maybeFriendEntry.getRelation_sina().length() > 0) {
				holder.weibo_layout.setVisibility(View.VISIBLE);
				holder.weibo_textview.setText(maybeFriendEntry
						.getRelation_sina());
			} else {
				holder.weibo_layout.setVisibility(View.GONE);
			}
			if (null != maybeFriendEntry.getRelation_tqq()
					&& maybeFriendEntry.getRelation_tqq().length() > 0) {
				holder.tencent_layout.setVisibility(View.VISIBLE);
				holder.tencent_textview.setText(maybeFriendEntry
						.getRelation_tqq());
			} else {
				holder.tencent_layout.setVisibility(View.GONE);
			}
			if (null != maybeFriendEntry.getRelation_renren()
					&& maybeFriendEntry.getRelation_renren().length() > 0) {
				holder.renren_layout.setVisibility(View.VISIBLE);
				holder.renren_textview.setText(maybeFriendEntry
						.getRelation_renren());
			} else {
				holder.renren_layout.setVisibility(View.GONE);
			}
			if (null != maybeFriendEntry.getRelation_douban()
					&& maybeFriendEntry.getRelation_douban().length() > 0) {
				holder.douban_layout.setVisibility(View.VISIBLE);
				holder.douban_textview.setText(maybeFriendEntry
						.getRelation_douban());
			} else {
				holder.douban_layout.setVisibility(View.GONE);
			}
			if (null != maybeFriendEntry.getRelation_qq()
					&& maybeFriendEntry.getRelation_qq().length() > 0) {
				holder.qq_layout.setVisibility(View.VISIBLE);
				holder.qq_textview.setText(maybeFriendEntry.getRelation_qq());
			} else {
				holder.qq_layout.setVisibility(View.GONE);
			}
			convertView.setTag(holder);
		} else {
			holder = (BTFriendViewHolder) convertView.getTag();
		}

		return convertView;
	}

	public static class BTFriendViewHolder {
		public ImageView mSex;
		RemoteImageView mAuthorView;
		ImageView mExType;

		TextView mNickName;
		TextView mDoing;
		TextView mLocation;
		Button mAddFriend;

		LinearLayout twitter_layout;
		LinearLayout facebook_layout;
		LinearLayout weibo_layout;
		LinearLayout tencent_layout;
		LinearLayout renren_layout;
		LinearLayout douban_layout;
		LinearLayout qq_layout;

		TextView twitter_textview;
		TextView facebook_textview;
		TextView weibo_textview;
		TextView tencent_textview;
		TextView renren_textview;
		TextView douban_textview;
		TextView qq_textview;

		TextView mTag;
		TextView mCommentLocation;
		Button mbtExPhoto;
		Button mbtExProfile;
		LinearLayout mFriendLocation;

		int flag;
	}

}
